#*************************************************************************
#Global variables for functions and term
#*************************************************************************
##import vnnlp

class Term:
    def __init__(self, rand):
        '''
        @functions: function set is list of [name, arity]
        @terminals: terminal set is list of [name, arity]
        '''
        self.termcard = 0 #function + terminal
        self.fcard = 0 #function
        self.tcard = 0 #terminal
        self.functions = []
        self.terminals = []
        self.terms = []
        self.random = rand

##    def __del__(self):
##        del self.functions
##        del self.terminals
##        del terms

    def AddTerm(self, name, arity):
        '''
        Add a function or a terminal
        '''
        self.terms.append([name, arity])
        self.termcard += 1
        if(arity==0): #terminal
            self.terminals.append([name, arity])
            self.tcard += 1
        else:
            self.functions.append([name, arity])
            self.fcard += 1

    def Swap(self, times):
        '''
        swap terms to make it self.random distributed
        '''
        assert (self.termcard >= 2)
        for i in range(times):
            j = self.random.randint(0, self.termcard-1)
            k = self.random.randint(0, self.termcard-1)
            #swap
            temp = self.terms[j]
            self.terms[j] = self.terms[k]
            self.terms[k] = temp

    def Print(self):
        n = len(self.termcard)
        for i in range(n):
            print '-------TERM ', i, '--------------'
            print 'NAME: ', self.terms[i][0]
            print 'ARITY: ', self.terms[i][1]
